<!DOCTYPE html><html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>高斯分布示意图</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
            color: #333;
        }
        h1 {
            text-align: center;
            color: #2c3e50;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        #chart-container {
            position: relative;
            margin: 20px 0;
        }
        canvas {
            display: block;
            margin: 0 auto;
            background: white;
            border: 1px solid #ddd;
        }
        .controls {
            display: flex;
            justify-content: center;
            gap: 15px;
            margin: 20px 0;
        }
        button {
            padding: 8px 15px;
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #2980b9;
        }
        .explanation {
            margin-top: 20px;
            line-height: 1.6;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>高斯分布 (正态分布) 示意图</h1>
        
        <div id="chart-container">
            <canvas id="gaussian-chart" width="700" height="400"></canvas>
        </div>
        
        <div class="controls">
            <button id="normal-btn">标准正态分布</button>
            <button id="wide-btn">较宽分布</button>
            <button id="narrow-btn">较窄分布</button>
            <button id="left-btn">左偏分布</button>
            <button id="right-btn">右偏分布</button>
            <button id="animate-btn">动画演示</button>
        </div>
        
        <div class="explanation">
            <h2>高斯分布简介</h2>
            <p>高斯分布，也称正态分布，是统计学中最重要的一种连续概率分布。其概率密度函数呈钟形曲线，由均值(μ)和标准差(σ)两个参数决定。均值决定了分布中心的位置，标准差决定了曲线的宽窄程度。</p>
            <p>在自然界和社会现象中，许多随机变量都服从或近似服从正态分布，如人类的身高、测量误差等。</p>
        </div>
    </div>

    <script>
        const canvas = document.getElementById('gaussian-chart');
        const ctx = canvas.getContext('2d');
        
        // 初始参数：均值=0，标准差=1（标准正态分布）
        let mu = 0;
        let sigma = 1;
        let animationId = null;
        
        // 绘制坐标轴
        function drawAxes() {
            const width = canvas.width;
            const height = canvas.height;
            const x0 = width / 2;
            const y0 = height - 50;
            
            ctx.strokeStyle = '#333';
            ctx.lineWidth = 1;
            
            // X轴
            ctx.beginPath();
            ctx.moveTo(50, y0);
            ctx.lineTo(width - 50, y0);
            ctx.stroke();
            
            // Y轴
            ctx.beginPath();
            ctx.moveTo(x0, height - 50);
            ctx.lineTo(x0, 50);
            ctx.stroke();
            
            // X轴刻度
            for (let i = -3; i <= 3; i++) {
                const x = x0 + i * 80;
                ctx.beginPath();
                ctx.moveTo(x, y0 - 5);
                ctx.lineTo(x, y0 + 5);
                ctx.stroke();
                
                ctx.font = '12px Microsoft YaHei';
                ctx.fillStyle = '#333';
                ctx.textAlign = 'center';
                ctx.fillText(i + 'σ', x, y0 + 20);
            }
            
            // Y轴标签
            ctx.save();
            ctx.translate(x0 - 30, height / 2);
            ctx.rotate(-Math.PI / 2);
            ctx.font = '14px Microsoft YaHei';
            ctx.fillStyle = '#333';
            ctx.fillText('概率密度', 0, 0);
            ctx.restore();
            
            // 分布参数显示
            ctx.font = '16px Microsoft YaHei';
            ctx.fillStyle = '#e74c3c';
            ctx.textAlign = 'left';
            ctx.fillText(`均值 μ = ${mu.toFixed(1)}  标准差 σ = ${sigma.toFixed(1)}`, 60, 30);
        }
        
        // 高斯函数
        function gaussian(x, mean, stddev) {
            const exponent = -Math.pow(x - mean, 2) / (2 * Math.pow(stddev, 2));
            return Math.exp(exponent) / (stddev * Math.sqrt(2 * Math.PI));
        }
        
        // 绘制高斯曲线
        function drawGaussian() {
            const width = canvas.width;
            const height = canvas.height;
            const x0 = width / 2;
            const y0 = height - 50;
            const scale = 300; // 放大因子使曲线更明显
            
            ctx.clearRect(0, 0, width, height);
            drawAxes();
            
            ctx.beginPath();
            ctx.strokeStyle = '#3498db';
            ctx.lineWidth = 2;
            
            for (let x = -4; x <= 4; x += 0.1) {
                const y = gaussian(x, mu, sigma) * scale;
                const plotX = x0 + x * 80;
                const plotY = y0 - y;
                
                if (x === -4) {
                    ctx.moveTo(plotX, plotY);
                } else {
                    ctx.lineTo(plotX, plotY);
                }
            }
            
            ctx.stroke();
            
            // 填充曲线下方区域
            ctx.fillStyle = 'rgba(52, 152, 219, 0.2)';
            ctx.lineTo(x0 + 4 * 80, y0);
            ctx.lineTo(x0 + -4 * 80, y0);
            ctx.closePath();
            ctx.fill();
        }
        
        // 动画效果：逐渐改变分布参数
        function animateTo(newMu, newSigma, duration = 2000) {
            const startTime = performance.now();
            const startMu = mu;
            const startSigma = sigma;
            
            if (animationId) {
                cancelAnimationFrame(animationId);
            }
            
            function animate(currentTime) {
                const elapsedTime = currentTime - startTime;
                const progress = Math.min(elapsedTime / duration, 1);
                
                mu = startMu + (newMu - startMu) * progress;
                sigma = startSigma + (newSigma - startSigma) * progress;
                
                drawGaussian();
                
                if (progress < 1) {
                    animationId = requestAnimationFrame(animate);
                } else {
                    animationId = null;
                }
            }
            
            animationId = requestAnimationFrame(animate);
        }
        
        // 初始绘制
        drawGaussian();
        
        // 按钮事件监听
        document.getElementById('normal-btn').addEventListener('click', () => {
            animateTo(0, 1);
        });
        
        document.getElementById('wide-btn').addEventListener('click', () => {
            animateTo(0, 2);
        });
        
        document.getElementById('narrow-btn').addEventListener('click', () => {
            animateTo(0, 0.5);
        });
        
        document.getElementById('left-btn').addEventListener('click', () => {
            animateTo(-1, 1);
        });
        
        document.getElementById('right-btn').addEventListener('click', () => {
            animateTo(1, 1);
        });
        
        document.getElementById('animate-btn').addEventListener('click', () => {
            const sequences = [
                { mu: 0, sigma: 1 },
                { mu: 0, sigma: 2 },
                { mu: -1.5, sigma: 2 },
                { mu: -1.5, sigma: 1 },
                { mu: 1.5, sigma: 1 },
                { mu: 1.5, sigma: 0.5 },
                { mu: 0, sigma: 0.5 },
                { mu: 0, sigma: 1 }
            ];
            
            let currentIndex = 0;
            const animateSequence = () => {
                if (currentIndex < sequences.length) {
                    const target = sequences[currentIndex];
                    animateTo(target.mu, target.sigma, 1000);
                    currentIndex++;
                    setTimeout(animateSequence, 1200);
                }
            };
            
            animateSequence();
        });
    </script>
</body>
</html>